﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using System.Collections.Generic;
using System.IO;
using ScrumTable.DL.Data.Generic;
using ScrumTable.DL.Data.Local.Properties;

#endregion

namespace ScrumTable.DL.Data.Local
{
    /// <summary>
    /// Specifies the data connection for a data sync enabled storage.
    /// This class represents the global connection state and should determine
    /// the connection to the local XML file.
    /// </summary>
    public class DataConnection : DataConnectionBase
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        private static readonly IDictionary<string, string> PathMappings = new Dictionary<string, string>
            {
                { "localhost", "LocalData.xml" }
            };

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        /// <summary>
        /// Creates a new DataConnection instance, no public constructor
        /// needed.
        /// </summary>
        internal DataConnection(DataContext context) : base(context)
        {
        }

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        /// <summary>
        /// Returns the default moniker if the provided moniker contains an invalid value.
        /// </summary>
        /// <param name="moniker">Specifies the moniker to validate.</param>
        /// <returns>Returns the default moniker for the current driver instance.</returns>
        protected override string ValidateMoniker(string moniker)
        {
            string validatedMoniker = DataContext.DriverInformation.MapDataFilePath(TryMapMoniker(moniker));

            if (!File.Exists(validatedMoniker))
            {
                throw new InvalidOperationException(string.Format(Resources.ExcInvalidMoniker, moniker, validatedMoniker));
            }
            return validatedMoniker;
        }

        private string TryMapMoniker(string moniker)
        {
            if (PathMappings.ContainsKey(moniker))
            {
                return PathMappings[moniker];
            }
            return moniker;
        }

        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------

        #endregion
    }
}